home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / mesademos / WINPOS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.6 KB  |  216 lines

  1. /* winpos.c */
  2.  
  3. /*
  4.  * Example of how to use the GL_MESA_window_pos extension.
  5.  *
  6.  * This program is in the public domain
  7.  *
  8.  * Brian Paul
  9.  */
  10.  
  11. /* Conversion to GLUT by Mark J. Kilgard */
  12.  
  13. /* The GL_MESA_window_pos extension lets you set the OpenGL raster position
  14.    (used for positioning output from glBitmap and glDrawPixels) based on
  15.    window coordinates (assuming an origin at the lower-left window corner).
  16.    Mesa has an extension to do this operation quickly, but the program
  17.    will emulate the raster position update if the extension is not available.
  18.    See the implementation of glWindowPos4fMESAemulate. -mjk */
  19.  
  20. #include <math.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <GL/glut.h>
  25.  
  26. #ifndef M_PI
  27. #define M_PI 3.14159265
  28. #endif
  29.  
  30. #define WIDTH 16
  31. #define HEIGHT 16
  32. int sizeX = WIDTH, sizeY = HEIGHT;
  33. GLubyte data[WIDTH * HEIGHT * 3];
  34.  
  35. /* glWindowPos4fMESAemulate & glWindowPos2fMESAemulate are lifted from the
  36.    Mesa 2.0 src/winpos.c emulation code. -mjk */
  37.  
  38. /*
  39.  * OpenGL implementation of glWindowPos*MESA()
  40.  */
  41. void 
  42. glWindowPos4fMESAemulate(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
  43. {
  44.   GLfloat fx, fy;
  45.  
  46.   /* Push current matrix mode and viewport attributes */
  47.   glPushAttrib(GL_TRANSFORM_BIT | GL_VIEWPORT_BIT);
  48.  
  49.   /* Setup projection parameters */
  50.   glMatrixMode(GL_PROJECTION);
  51.   glPushMatrix();
  52.   glLoadIdentity();
  53.   glMatrixMode(GL_MODELVIEW);
  54.   glPushMatrix();
  55.   glLoadIdentity();
  56.  
  57.   glDepthRange(z, z);
  58.   glViewport((int) x - 1, (int) y - 1, 2, 2);
  59.  
  60.   /* set the raster (window) position */
  61.   fx = x - (int) x;
  62.   fy = y - (int) y;
  63.   glRasterPos4f(fx, fy, 0.0, w);
  64.  
  65.   /* restore matrices, viewport and matrix mode */
  66.   glPopMatrix();
  67.   glMatrixMode(GL_PROJECTION);
  68.   glPopMatrix();
  69.  
  70.   glPopAttrib();
  71. }
  72.  
  73. void
  74. glWindowPos2fMESAemulate(GLfloat x, GLfloat y)
  75. {
  76.   glWindowPos4fMESAemulate(x, y, 0, 1);
  77. }
  78.  
  79. /* Make cheesy pixel image for glDrawPixels. */
  80. static void
  81. init(void)
  82. {
  83.   int i, j;
  84.   static char *pattern[16] =
  85.   {
  86.     "    ........    ",
  87.     "   ..........   ",
  88.     "  ...xx..xx...  ",
  89.     "  ............  ",
  90.     "   ...oooo...   ",
  91.     "*   ........    ",
  92.     "*oooooooooooooo*",
  93.     "      oooo     *",
  94.     "      oooo      ",
  95.     "      xxxx      ",
  96.     "      xxxx      ",
  97.     "     xx  xx     ",
  98.     "    xx    xx    ",
  99.     "   xx      xx   ",
  100.     "   **      **   ",
  101.     "  ***      ***  ",
  102.   };
  103.   GLubyte red, green, blue;
  104.  
  105.   /* Generate a pixel image from pattern. */
  106.   for (i = 0; i < HEIGHT; i++) {
  107.     for (j = 0; j < WIDTH; j++) {
  108.       switch (pattern[HEIGHT - i - 1][j]) {
  109.       case '.':
  110.         red = 0xff;
  111.         green = 0xff;
  112.         blue = 0x00;
  113.         break;
  114.       case 'o':
  115.         red = 0xff;
  116.         green = 0x00;
  117.         blue = 0x00;
  118.         break;
  119.       case 'x':
  120.         red = 0x00;
  121.         green = 0xff;
  122.         blue = 0x00;
  123.         break;
  124.       case '*':
  125.         red = 0xff;
  126.         green = 0x00;
  127.         blue = 0xff;
  128.         break;
  129.       case ' ':
  130.         red = 0x00;
  131.         green = 0x00;
  132.         blue = 0x00;
  133.         break;
  134.       }
  135.       data[(i * WIDTH + j) * 3 + 0] = red;
  136.       data[(i * WIDTH + j) * 3 + 1] = green;
  137.       data[(i * WIDTH + j) * 3 + 2] = blue;
  138.     }
  139.   }
  140. }
  141.  
  142. #ifdef GL_MESA_window_pos
  143. int emulate;
  144. #endif
  145.  
  146. static void
  147. draw(void)
  148. {
  149.   GLfloat angle;
  150.  
  151.   glClear(GL_COLOR_BUFFER_BIT);
  152.  
  153.   for (angle = -45.0; angle <= 135.0; angle += 10.0) {
  154.     GLfloat x = 50.0 + 200.0 * cos(angle * M_PI / 180.0);
  155.     GLfloat y = 50.0 + 200.0 * sin(angle * M_PI / 180.0);
  156.  
  157. #ifdef GL_MESA_window_pos
  158.     /* Don't need to worry about the modelview or projection
  159.        matrices!!! */
  160.     if (!emulate)
  161.       glWindowPos2fMESA(x, y);
  162.     else
  163. #endif
  164.       glWindowPos2fMESAemulate(x, y);
  165.     glDrawPixels(sizeX, sizeY, GL_RGB,
  166.       GL_UNSIGNED_BYTE, data);
  167.   }
  168.   glFlush();
  169. }
  170.  
  171. /* ARGSUSED1 */
  172. static void
  173. key(unsigned char k, int x, int y)
  174. {
  175.   switch (k) {
  176.   case 27:
  177.     exit(0);
  178.   }
  179. }
  180.  
  181. int
  182. main(int argc, char *argv[])
  183. {
  184.   glutInitWindowSize(500, 500);
  185.   glutInit(&argc, argv);
  186.   glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  187.  
  188.   glutCreateWindow("winpos");
  189.  
  190.   if (glutExtensionSupported("GL_MESA_window_pos")) {
  191.     printf("OpenGL implementation supports Mesa's GL_MESA_window_pos extension.");
  192. #ifdef GL_MESA_window_pos
  193.     emulate = 0;
  194. #else
  195.     printf("(Not compiled to support the extension.)\n");
  196.     printf("Emulating...\n");
  197. #endif
  198.   } else {
  199.     printf("Sorry, GL_MESA_window_pos extension not available.\n");
  200.     printf("Emulating...\n");
  201. #ifdef GL_MESA_window_pos
  202.     emulate = 1;
  203. #else
  204.     printf("(Not compiled to support the extension even if available.)\n");
  205. #endif
  206.   }
  207.  
  208.   init();
  209.  
  210.   glutDisplayFunc(draw);
  211.   glutKeyboardFunc(key);
  212.  
  213.   glutMainLoop();
  214.   return 0;             /* ANSI C requires main to return int. */
  215. }
  216.